<?php

/**
 * ==============================================
 * Created by Finger Technology.
 * Author: ZhiHuaWei <zhihua_wei@foxmail.com>
 * Date: 2019/8/26 17:40
 * Project: YunAdmin
 * Power: YunAdmin 文章类别控制器
 * Version: 1.0.0
 * ==============================================
 */

namespace app\admin\controller;

use think\Db;

/**
 * 文章类别类
 * Class Category
 * @package app\admin\controller
 */
class Category extends YunAdmin
{
    /**
     * 分类管理
     * @return mixed
     */
    public function index()
    {
        $cate_list = Db::name('category')->field('category_id,category_name,keywords,sort,create_time,modify_time')->where(['is_delete' => 0])->order("sort ASC")->paginate(10);
        $this->assign('list', $cate_list);
        return $this->fetch();
    }

    /**
     * 新增分类
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 修改分类
     * @return mixed
     */
    public function edit()
    {
        $id = input('id', 0);
        if ($id) {
            $category_info = Db::name('category')->field('category_id,category_name,keywords,sort,category_desc')->where(['category_id' => $id])->find();
            $this->assign('category', $category_info);
            return $this->fetch();
        } else {
            $this->error('系统异常，请重试！');
        }
    }

    /**
     * 保存分类
     */
    public function save()
    {
        # 1 获得提交分类信息
        $category_info = [
            'category_name' => input('post.category_name', ''),
            'keywords' => input('post.keywords', ''),
            'sort' => input('post.sort', 0),
            'category_desc' => input('post.category_desc', ''),
            'modify_time' => date("Y-m-d H:i:s"),
        ];

        # 2 根据是否有id判断是新增还是修改
        $id = input('post.id', 0);
        if ($id) {
            # 3 修改文章分类信息
            $ret = Db::name('category')->where(['category_id' => $id])->update($category_info);
            if ($ret !== false) {
                $this->success('恭喜您，文章分类信息修改成功！', url('index'));
            } else {
                $this->error('文章分类信息修改失败，请重试！');
            }
        } else {
            # 4 新增文章分类信息
            $category_info['create_time'] = $category_info['modify_time'];
            $ret = Db::name('category')->insert($category_info);
            if ($ret !== false) {
                $this->success('恭喜您，文章分类信息新增成功！', url('index'));
            } else {
                $this->error('文章分类信息新增失败，请重试！');
            }
        }
    }

    /**
     * 删除分类
     */
    public function del()
    {
        # 1 获取需删除的类别id
        $id = input('id', 0);
        if ($id) {

            # 2 判断分类下是否有文章
            $article_count = Db::name('article')->where(['category_id' => $id, 'is_delete' => 0])->count();
            if($article_count > 0){
                $this->error('此分类下还存在文章，不允许删除！');
            }

            # 3 进行软删除操作
            $category_del = [
                'is_delete' => 1,
                'modify_time' => date("Y-m-d H:i:s"),
            ];
            $ret = Db::name('category')->where(['category_id' => $id])->update($category_del);
            # 4 结果返回
            if ($ret !== false) {
                $this->success('恭喜您，文章分类信息删除成功！', url('index'));
            } else {
                $this->error('文章分类信息删除失败，请重试！');
            }
        } else {
            $this->error('系统异常，请重试！');
        }
    }

}